<template>
  <div class="system-role-container layout-padding">

    <div class="system-role-padding layout-padding-auto layout-padding-view">
      <div class="system-user-search mb15">
        <el-input size="default" v-model="state.keyword" placeholder="请输入关键字" style="max-width: 180px" clearable> </el-input>
        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon> <ele-Search /> </el-icon>
          查  询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenDialog('add')">
          <el-icon><ele-CirclePlusFilled /></el-icon>
          新  增
        </el-button>
        <el-button size="default" type="danger" class="ml10" @click="onRowDel()">
          <el-icon><ele-DeleteFilled /></el-icon>
          删  除
        </el-button>
      </div>

      <el-table :data="state.tableData.data" ref="multipleTable" v-loading="state.tableData.loading"
                style="width: 100%"
                row-key="id"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="selection" width="50" />
<!--        <el-table-column type="index" label="序号" width="60" />-->
          <el-table-column prop="data.name" label=" 名称" show-overflow-tooltip>
            <template #default="scope">
              <SvgIcon :name="scope.row.meta.icon" />
              <span class="ml10">{{scope.row.data.name}}</span>
            </template>
          </el-table-column>
        <el-table-column prop="data.descript" label="描述" show-overflow-tooltip></el-table-column>
        <el-table-column prop="data.state" label="状态" show-overflow-tooltip  width="100">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.data.state==1">启用</el-tag>
            <el-tag type="info" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="data.modifyUser" label="更新人" show-overflow-tooltip width="100"></el-table-column>
        <el-table-column prop="data.modifyDate" label="更新时间" show-overflow-tooltip width="180"></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenDialog('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="system-dic-dialog-container">
      <el-dialog class="system-dialog" :title="state.dialog.title" v-model="state.dialog.isShowDialog" draggable>
        <el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
          <el-row :gutter="35">


            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="父类">
                <el-cascader ref="deptTreeRef" v-model="state.ruleForm.parentId" placeholder="请选择父类" clearable
                             :options="state.dialog.deptOptions"
                             :props="{checkStrictly:true, value:'id',label:'text' }"
                />
              </el-form-item>
            </el-col>
           
             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label=" 名称">
                 <el-input v-model="state.ruleForm.name" placeholder="请输入 名称" clearable></el-input>
               </el-form-item>
             </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="状态">
                <el-switch v-model="state.ruleForm.state" :active-value="1" inline-prompt active-text="启用" inactive-text="禁用"></el-switch>
              </el-form-item>
            </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="描述">
                <el-input v-model="state.ruleForm.descript" placeholder="请输入描述" clearable></el-input>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
        <template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialog.isShowDialog=false" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
        </template>
      </el-dialog>
    </div>

  </div>
</template>

<script setup lang="ts" name="systemDic">
import {reactive, onMounted, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import { api } from '/@/api/system/admin_dept_api.ts';
import commonUtil from "/@/utils/commonUtil";

// 定义变量内容
const multipleTable = ref();
const deptTreeRef = ref();
interface RowData {
    id?: string;
    operateType?: number;
   parentId?: string;
   name?: string;
  state?: number;
   mark?: string;
 
}

interface Node {
  check? : boolean;
  children? : [Node];
  data? : RowData;
  id?: string;
  leaf?: boolean;
  nodeType?: string;
  parentId?: string;
  text?: string;
}

// 数据绑定
const state = reactive({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
  keyword: '',
  ruleForm: <RowData>({}),
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
    deptOptions: [],
  },
});

//-------------------------------------------------------------------
// 打开弹窗
const onOpenDialog = (type: string, row ?: Node) => {
  if (type === 'edit') {
    state.ruleForm = <RowData>(row?.data);
    state.dialog.title = '修改系统部门';
    state.dialog.submitTxt = '修 改';
  } else {
    state.dialog.title = '新增系统部门';
    state.dialog.submitTxt = '新 增';
    state.ruleForm = <RowData>({});
  }
  state.dialog.isShowDialog = true;
};
// 提交
const onSubmit = () => {
  state.dialog.isShowDialog = false;
  //保存
  var data = state.ruleForm;
  var isAdd = data.id ? false : true;
  data.operateType = (isAdd ? 1 : 2);

  let arr = commonUtil.getCascaderValues(deptTreeRef);
  data.parentId = (arr!=undefined && arr?.length>0 ? arr[0] : '0') ;

  state.tableData.loading = true;
  api().operate([data]).then((res)=>{
    if(commonUtil.hasErr(res, (isAdd?'新增':'修改') , true )) {return;}
    getTableData();
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {state.tableData.loading = false;})
};
// 删除
const onRowDel = (param ?: RowData) => {
  //获取选中的id
  let list: RowData[] = []
  if(param){
    list.push(param)
  }else{
    const selectionRows = multipleTable.value.getSelectionRows();
    if(!selectionRows || selectionRows.length == 0){
      ElMessage.error('请选择要操作的数据');
      return
    }
    list = selectionRows
  }
  // 设置值
  for (let i = 0; i < list.length; i++) {
    list[i].operateType=3; //删除
  }
  // 弹出框
  ElMessageBox.confirm(`是否永久删除 ?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    state.tableData.loading = true;
    api().operate(list).then((res)=>{
      if(commonUtil.hasErr(res, '删除' , true )) {return;}
        getTableData();
    }).catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
      .finally(() => {state.tableData.loading = false;})
  }).catch(() => {});
};

//-------------------------------------------------------------------
// 查询表格数据
const getTableData = (isInit? : boolean) => {
  state.tableData.loading = true;
  const param = {
    keyword: state.keyword,
    nodeText: '顶级部门'
  }
  api().list(param).then(res=>{
    if(commonUtil.hasErr(res, '查询' , false )) {return;}
    state.tableData.data = res.treeNode.children;
    isInit && (state.dialog.deptOptions = [res.treeNode]);
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {state.tableData.loading = false;})
};

//-------------------------------------------------------------------
// 页面加载时
onMounted(() => {
  getTableData(true);
});

</script>


